package com.myph.model;



import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;

public class HttpUtil {

	public static boolean isNetworkConnected(Context context) {
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
			if (mNetworkInfo != null) {
				return mNetworkInfo.isConnected();
			}
		}
		return false;
	}

	/**
	 * 
	 * 
	 * @param context
	 * @param fileName
	 * @param content
	 */
	public static void saveJsonData(Context context, String fileName, JSONObject content) {
		if (content != null) {
			try {
				FileOutputStream outputStream = context.openFileOutput(fileName, Activity.MODE_PRIVATE);
				outputStream.write(content.toString().getBytes());
				outputStream.flush();
				outputStream.close();
				// System.out.println("to sdCard:"+content.toString().getBytes());
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void saveListData(Context context, String fileName, List<String> content) {
		if (content != null) {
			try {
				FileOutputStream outputStream = context.openFileOutput(fileName, Activity.MODE_PRIVATE);
				outputStream.write(content.toString().getBytes());
				outputStream.flush();
				outputStream.close();
				// System.out.println("to sdCard:"+content.toString().getBytes());
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 
	 * 
	 * @param context
	 * @param fileName
	 * @return
	 */
	public static JSONObject readJsonData(Context context, String fileName) {
		try {
			FileInputStream inputStream = context.openFileInput(fileName);
			byte[] bytes = new byte[1024 * 3];
			ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
			while (inputStream.read(bytes) != -1) {
				arrayOutputStream.write(bytes, 0, bytes.length);
			}
			inputStream.close();
			arrayOutputStream.close();
			String content = new String(arrayOutputStream.toByteArray());

			JSONObject obj;
			try {
				obj = new JSONObject(content);
				return obj;
			} catch (JSONException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static List<String> readListData(Context context, String fileName) {
		try {
			FileInputStream inputStream = context.openFileInput(fileName);
			byte[] bytes = new byte[1024 * 3];
			ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
			while (inputStream.read(bytes) != -1) {
				arrayOutputStream.write(bytes, 0, bytes.length);
			}
			inputStream.close();
			arrayOutputStream.close();
			String content = new String(arrayOutputStream.toByteArray());

			List<String> obj=new ArrayList<String>();
			JSONArray jsonArray=null;
			try {
				jsonArray = new JSONArray(content);
				for (int i = 0; i < jsonArray.length(); i++) {
					obj.add(jsonArray.getString(i));
				}
				return obj;
			} catch (JSONException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static JSONObject httpGetJsonData(String uri) {
		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpGet get = new HttpGet(uri);
			HttpResponse response;
			response = httpclient.execute(get);
			int code = response.getStatusLine().getStatusCode();

			if (code == 200) {
				String rev = EntityUtils.toString(response.getEntity());
				JSONObject obj = new JSONObject(rev);
				return obj;
			}
		} catch (Exception e) {
			// System.out.println(e.getMessage());
		}
		return null;
	}

	/**
	 * 
	 * 
	 * @param url
	 * @return
	 */
	public static Bitmap getHttpBitmap(String url) {
		URL myFileURL;
		Bitmap bitmap = null;
		try {
			myFileURL = new URL(url);

			HttpURLConnection conn = (HttpURLConnection) myFileURL.openConnection();
		
			conn.setConnectTimeout(6000);

			conn.setDoInput(true);

			conn.setUseCaches(false);

			// conn.connect();

			InputStream is = conn.getInputStream();

			bitmap = BitmapFactory.decodeStream(is);

			is.close();
		} catch (Exception e) {
			// System.out.println("bit_error1" + e.toString());
		}
		return bitmap;
	}
	
	/**
	 * 
	 * @return
	 */
	public static boolean isWifiConnected(Context context) {
		WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		return manager.isWifiEnabled();
	}
}

